package com.aplink.dellcommercial.fragments;

import android.os.Bundle;
import android.os.Message;

import com.aplink.dellcommercial.R;
import com.aplink.dellcommercial.configuration.DellController;
import com.aplink.dellcommercial.database.DellDataTable;
import com.aplink.dellcommercial.util.DellCommercialUtility;
import com.aplink.dellcommercial.webservices.DataResult;
import com.aplink.dellcommercial.webservices.WebServiceHelper;
import com.aplink.generic.GenericFragment;
import com.aplink.generic.constant.HandlerConstant;
import com.aplink.generic.constant.SqlDataType;
import com.aplink.generic.database.SqlMethods;
import com.aplink.generic.handler.IHandleMessage;
import com.aplink.generic.pojo.GenericObject;
import com.aplink.generic.pojo.ObjPropValue;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.util.validation.ControlValidation;

import java.util.ArrayList;

public class ChangePasswordFragment extends GenericFragment implements
        IHandleMessage {
    private String mNewPassword;

    @Override
    public int getFormDesign() {
        return R.raw.form_design_change_password;
    }

    @Override
    public int getContentData() {
        return R.raw.content_data_new_login;
    }

    @Override
    public String getTable() {
        return DellDataTable.UserLoginInfo.TABLE_NAME;
    }

    @Override
    public void onActivityCreated( Bundle savedInstanceState ) {
        // TODO Auto-generated method stub
        super.onActivityCreated( savedInstanceState );
        this.initData();
        this.loadDataToForm();
        this.getHandler().setHandleMessage( this );
    }

    @Override
    protected void onHandleControlClickEvent( ControlSpec controlSpec ) {
        String validationGroup = controlSpec.getValidationGroup();
        if ( validationGroup.equalsIgnoreCase( "change_pass" ) ) {
            if ( ControlValidation.isFormValidate( this ) ) {
                ArrayList<ObjPropValue> objPropValues = getObjPropValues();
                String password1 = (String) objPropValues.get( 0 ).getAttribute()
                        .getValue();
                String password2 = (String) objPropValues.get( 1 ).getAttribute()
                        .getValue();
                if ( password1.equals( password2 ) ) {
                    String email = DellController.getInstance()
                            .getAccountEmail();
                    WebServiceHelper.getInstance().changePassword(
                            getActivity(), getHandler(), email, password1 );
                    mNewPassword = password1;
                } else {
                    DellCommercialUtility.showAlertDialog(
                            getApplicationContext(),
                            android.R.drawable.ic_dialog_alert,
                            getString( R.string.error_enter_math_pass ),
                            getString( R.string.change_password ) );
                }
            }
        }
    }

    @Override
    public int getContentView() {
        return R.layout.aplink_dellcommercial_change_password;
    }

    @Override
    public void onHandleMessage( Message msg ) {
        if ( msg.what == HandlerConstant.HTTP_TASK_RESULT ) {
            final DataResult result = (DataResult) msg.obj;
            int resultCode = (Integer) result.getData();
            if ( resultCode == 1 ) {
                String contactId = DellController.getInstance().getContactID();

                String whereClause = SqlMethods.getInstance().equal( SqlDataType.INTEGER,
                        DellDataTable.UserLoginInfo.CONTACT_ID, contactId ).toString();

                GenericObject userInfo = this.selectCommand().get(
                        getObjPropCol(), whereClause );

                String mappingName = getObjPropValues().get( 0 )
                        .getObjectPropID();
                userInfo.setContentByAttributeName( mappingName, mNewPassword );
                insertCommand().insertOrUpdate( userInfo, getObjPropCol() );
                DellCommercialUtility.logout( getActivity() );
                finishActivity();
            }
        }
    }

}
